package com.xl.web.service.sys;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xl.base.annotation.DataSource;
import com.xl.base.service.BaseService;
import com.xl.core.constant.Globals;
import com.xl.core.enums.CommonEnum;
import com.xl.core.enums.SourceEnum;
import com.xl.core.util.PasswordUtil;
import com.xl.core.util.SessionLocalUtils;
import com.xl.core.util.StringUtil;
import com.xl.web.dao.sys.SysMemberDao;
import com.xl.web.dao.sys.SysRoleDao;
import com.xl.web.entity.sys.SysMember;
import com.xl.web.entity.sys.SysRole;
import com.xl.web.entity.sys.SysRoleMember;

/**
 * 用户数据操作Service
 * 
 * @author 梁华
 * 
 * @param <T>
 */
@Service("memberService")
public class SysMemberService<T extends SysMember> extends
		BaseService<SysMember> {

	// 用户数据Dao
	@Autowired(required = false)
	private SysMemberDao<SysMember> memberDao;

	// 角色数据Dao
	@Autowired(required = false)
	private SysRoleDao<SysRole> sysRoleDao;

	public SysMemberDao<SysMember> getDao() {
		return memberDao;
	}

	/**
	 * 根据用户名获取用户信息
	 * 
	 * @param name
	 * @return
	 * @throws Exception
	 */
	@DataSource(source = SourceEnum.TYPE_SLAVE)
	public SysMember queryByName(Object name) throws Exception {
		return getDao().queryByName(name);
	}

	/**
	 * 根据手机号获取用户信息
	 * 
	 * @param name
	 * @return
	 * @throws Exception
	 */
	@DataSource(source = SourceEnum.TYPE_SLAVE)
	public SysMember queryByMobile(Object mobile) throws Exception {
		return getDao().queryByMobile(mobile);
	}

	/**
	 * 根据角色Id获取用户信息
	 * 
	 * @param name
	 * @return
	 * @throws Exception
	 */
	@DataSource(source = SourceEnum.TYPE_SLAVE)
	public List<SysMember> selectByRoleId(Object roleId) throws Exception {
		return getDao().selectByRoleId(roleId);
	}

	/**
	 * 根据管理id获得用户信息
	 * 
	 * @param name
	 * @return
	 * @throws Exception
	 */
	@DataSource(source = SourceEnum.TYPE_SLAVE)
	public SysMember queryByItemId(SysMember sysMember) throws Exception {
		return getDao().queryByItemId(sysMember);
	}

	/**
	 * 保存或者更新账户信息
	 * 
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public void addOrUpdate(SysMember member, String checkedRole)
			throws Exception {

		// 新增人员保存
		if (StringUtil.isEmpty(member.getId())) {

			// 密码验证
			String password = PasswordUtil.encrypt(member.getMemName(),
					member.getMemPwd(), PasswordUtil.getStaticSalt());
			member.setMemPwd(password);
			member.setMemParentId(SessionLocalUtils.getOperator().getMember()
					.getId());
			super.add(member);
		} else {
			// 更新人员保存
			super.updateBySelective(member);
			member = super.queryById(member);
		}

		// 删除原先分配的记录
		sysRoleDao.deleteRoleMemberByMemberId(member.getId());

		// 保存选择的菜单
		if (!StringUtil.isEmpty(checkedRole)) {

			String[] roleList = checkedRole.split(Globals.COMMA);
			SysRoleMember roleMember = null;
			for (String roleId : roleList) {
				roleMember = new SysRoleMember();
				roleMember.setRoleId(roleId);
				roleMember.setMemberId(member.getId());
				roleMember.preInsert();
				sysRoleDao.addRoleMember(roleMember);
			}
		}
	}

	/**
	 * 删除数据
	 */
	@Override
	public void delete(Object... ids) throws Exception {
		if (ids == null || ids.length < 1) {
			return;
		}
		// 删除缓存中对应所属关系
		for (Object id : ids) {
			this.sysRoleDao.deleteRoleMemberByMemberId(id);
			getDao().delete(id);
		}
	}

	/**
	 * 用户注册
	 * 
	 * @param member
	 * @throws Exception
	 */
	public void regist(SysMember sysMember) throws Exception {

		// 对密码进行二次加密保存
		String password = PasswordUtil.encrypt(sysMember.getMemName(),
				sysMember.getMemPwd(), PasswordUtil.getStaticSalt());
		// 设置用户初始状态未不可用
		sysMember.setMemStatus(CommonEnum.mem_status.STATUS_2.getValue());
		sysMember.setMemPwd(password);
		// 保存用户数据
		super.add(sysMember);
	}
}
